import subprocess
import re

# 检查待发布包的版本号
with open('pyproject.toml', 'r', encoding='utf-8') as file:
    line = file.readline()
    while line:
        if "version" in line:
            local_version_str = line
        line = file.readline()
# 匹配双引号之间的内容
local_version = re.findall(r'"([^"]*)"', local_version_str)

result = subprocess.run('pip index versions tfc_toolbox_py',capture_output=True, text=True)

package_version_str = result.stdout
# 匹配括号之间的内容
package_version = re.findall(r'\((.*?)\)', package_version_str)
version = local_version[0]
print(f"待发布包的版本：{version}")

# 构建包
result = subprocess.run('py -m build')
# 通过构建包安装
subprocess.run(f"pip install tfc_toolbox_py-{version}-py3-none-any.whl  --force-reinstall", cwd='C:\\Users\\TFC\\Documents\\MyProjects\\tfc_toolbox_py\\dist', check=True)
print(result.returncode) # 返回码
print(result.stdout) # 标准输出
print(result.stderr) # 标准错误